package com.xcjh.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.xcjh.domain.DataAcquisition;
import com.xcjh.dto.BaseMember;
import com.xcjh.dto.marray.MarrayMain;
import com.xcjh.eums.AppType;
import com.xcjh.service.DataAcquisitionService;
import com.xcjh.service.MarrayuService;
import com.xcjh.websocket.WebSocketSever;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MarrayuServiceImpl implements MarrayuService {
    @Autowired
    private DataAcquisitionService dataAcquisitionService;

    @Override
    public void save(String body) {
        body=body.substring(body.indexOf("{"));
        MarrayMain marrayMain = JSONUtil.toBean(body, MarrayMain.class);
        String name=buildName(marrayMain);
        String org=buildOrg(marrayMain);
        String avatar=marrayMain.getUser().getAvatar();
        String birthday=marrayMain.getUser().getBirthday();
        String birthdayStr=YiduiServiceImpl.toViewDate(birthday);
        String finalStr="";
        if(org!=null){
            birthdayStr=birthdayStr==null?"":"你是"+birthdayStr+",比我小一点 年龄刚合适";
            String nameStr=StrUtil.isEmpty(name)?"哈喽":"我的"+name+"妹妹";
            finalStr=nameStr+","+org+" 我挺喜欢你的,"+birthdayStr+". 我等你回消息,赶紧回个消息 ,加我18200158896 ";
        }else{
            birthdayStr=birthdayStr==null?"":"她说你是"+birthdayStr+",年龄刚好";
            String nameStr=StrUtil.isEmpty(name)?"哈喽":"小"+name+"妹妹";
            finalStr=nameStr+",你是我喜欢的类型,我刚刚让红娘给我们连线,没在线呢?. "+birthdayStr+" 赶紧回个消息,加我18200158896 ";
        }

        BaseMember msg=new BaseMember();
        msg.setAvatar_url(avatar);
        if(findOrSave(marrayMain)==null){
            msg.setViewStr(finalStr);
        }else{
            msg.setViewStr("@@@@@@已经有了@@@@@@@");
        }
        WebSocketSever.sendMessageByUser("marray", JSONUtil.toJsonStr(msg));

    }

    private DataAcquisition findOrSave(MarrayMain marrayMain){
        //所有数据,不管发没发送过
        List<DataAcquisition> dbAll = dataAcquisitionService.lambdaQuery()
                .eq(DataAcquisition::getAppId, marrayMain.getUser().getUid().toString())
                .eq(DataAcquisition::getType, AppType.MARRAY.getMessage())
                .list();
        if(CollectionUtil.isNotEmpty(dbAll)){
            return dbAll.get(0);
        }
        DataAcquisition d=new DataAcquisition();
        d.setAppId(marrayMain.getUser().getUid().toString());
        d.setSend(true);
        d.setType(AppType.MARRAY.getMessage());
        d.setDemoJson(JSONUtil.toJsonStr(marrayMain));
        dataAcquisitionService.save(d);
        return null;
    }

    private String buildOrg(MarrayMain marrayMain) {
        if(marrayMain==null||marrayMain.getAuth()==null||marrayMain.getAuth().getJob()==null){
            return null;
        }
        String company=marrayMain.getAuth().getJob().getName();
        company=company.replace("成都市","")
                .replace("成都","")
                .replace("四川省","")
                .replace("四川","")
                .replace("有限公司","")
                .replace("股份","")
        ;
        if(StrUtil.isEmpty(company)){
            return null;
        }
        return "我之前也在"+company+"干过,对我还有印象不?";
    }

    private String buildName(MarrayMain marrayMain){
        if(marrayMain==null||marrayMain.getAuth()==null||marrayMain.getAuth().getIdentity()==null){
            return "";
        }
        return marrayMain.getAuth().getIdentity().getName().replaceAll("\\*","");
    }

}
